<?php

/**
 * 用户信息管理
 *
 * @author sheyuhang <1464887278@qq.com>
 */

use \Passport\Account as Account;
use \Center\User as CUser;
use Passport\Login;

define('SITE_AUTH', 'passport-user');
require('../../../common.inc.php');

class user extends \lib\core\AdminAction
{
    public function doDefault()
    {
        $page = $this->app->page();
        $page->output();
    }

    /**
     * 数据类型转换
     * $gender 性别
     * $status 状态
     * $user_type 用户类型
     */
    private static $gender = [
        0 => '女',
        1 => '男',
        2 => '未填写'
    ];

    private static $status = [
        0 => '禁止',
        1 => '正常'
    ];

    //    private static $user_type = [
    //        0 => '未填写',
    //        1 => '备孕',
    //        2 => '怀孕',
    //        3 => '辣妈',
    //        4 => '经期'
    //    ];

    /**
     * 搜索
     */
    public function doSearch()
    {

        $user_id = intval($_POST['user_id']);
        $mobile = trim($_POST['mobile']);
        $name = trim($_POST['name']);

        if (empty($user_id)) {
            //手机号查询（通行证）
            $cm = Passport\Login::isMobileExists($mobile);
            if (!empty($cm)) {
                $user_id = $cm;
            }
            if (empty($cm)) {
                //用户名查询（通行证）
                $cn = Login::checkUserName($name);
                if (!empty($cn)) {
                    $user_id = $cn;
                }
            }
        }

        $fulldetail = CUser::getUserById($user_id);
        $check = Account::getUserById($fulldetail['UserId']);
        if (!empty($fulldetail['UserId'])) {
            $fulldetail['LoginMobile'] = $check['Mobile'];
            $fulldetail['Gender'] = self::$gender[$fulldetail['Gender']];
            $fulldetail['Status'] = self::$status[$fulldetail['Status']];
            $fulldetail['Avatar'] = $fulldetail['AvatarUrl'];
            $fulldetail['LoginTime'] = $check['LoginTime'];
            $this->output(['status' => true, 'data' => $fulldetail]);
        } else {
            $this->output(['status' => false, 'msg' => '该用户不存在！']);
        }
    }

    /**
     * 基本信息修改
     */
    public function doUpdate()
    {
        $user_id = intval($_POST['user_id']);
        if (!$user_id) {
            $this->output(['status' => false, 'msg' => '用户ID不能为空！']);
        }

        // 密码修改
        if (isset($_POST['password'])) {
            $password = trim($_POST['password']);
            if (empty($password)) {
                $this->output(['status' => false, 'msg' => '重设用户密码不能为空！']);
            }

            $result = Account::updatePassword($user_id, $password);
            if ($result) {
                $this->adminLog('修改密码', $user_id, 'update_password', '');
                $this->output(['status' => true, 'msg' => '密码修改成功！']);
            } else {
                $this->output(['status' => false, 'msg' => '操作失败， 请稍后再试！']);
            }
        }
    }
}

$app->run();